package com.losers;

import java.io.ByteArrayOutputStream;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Beer {
	private String name;
	private String brewery;
	private String description;
	private String abv;
	private float rating;
	private int type;
	private Bitmap picture = null;
	
	public String toString() {
		return name;
	}
	
	public String getName() { return name; }	
	public void setName(String name) { this.name = name; }
	
	public String getBrewery() { return brewery; }
	public void setBrewery(String brewery) { this.brewery = brewery; }
	
	public String getAbv() { return abv; }
	public void setAbv(String abv) { this.abv = abv; }
	
	public float getRating() { return rating;	}
	public void setRating(float rating) {	this.rating = rating; }
	
	public int getType() { return type; }
	public void setType(int type) {	this.type = type; }
	
	public String getDescription() { return description; }	
	public void setDescription(String description) { this.description = description; }	
	
	public Bitmap getPicture() { return picture; }
	public void setPicture(Bitmap bmp) { picture = bmp; }
	
	static Cursor getAll(SQLiteDatabase db) {
		return db.rawQuery("SELECT * FROM beer ORDER BY name", null);
	}
	
	static public Beer getById(String id, SQLiteDatabase db) {
		// Create empty beer
		Beer beer = new Beer();
		
		// Try to find beer from database
		Cursor c = db.rawQuery("SELECT * FROM beer WHERE _id=?", new String[]{id});
		if (c.moveToFirst()) {
			// Fill beer data
			beer.name = c.getString(c.getColumnIndex("name"));
			beer.brewery = c.getString(c.getColumnIndex("brewery"));
			beer.abv = c.getString(c.getColumnIndex("abv"));
			beer.description = c.getString(c.getColumnIndex("description"));
			beer.rating = c.getFloat(c.getColumnIndex("rating"));
			beer.type = c.getInt(c.getColumnIndex("type"));
			//beer.picture = c.getBlob(c.getColumnIndex("picture"));
			
			byte[] bytes = c.getBlob(c.getColumnIndex("picture"));
			if (bytes != null) {
				beer.picture = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
				Log.w("jotain", "jotain");
			}
		    
			

		} 
		c.close();
 
		return beer;
	}
	
	private ContentValues createContentValues() {
		ContentValues cv = new ContentValues();
		 
		cv.put("name", name);
		cv.put("brewery", brewery);
		cv.put("abv", abv);
		cv.put("description", description);
		cv.put("type", type);
		cv.put("rating", rating);
        
		if (picture != null)
		{
			ByteArrayOutputStream out = new ByteArrayOutputStream(); 
			picture.compress(Bitmap.CompressFormat.PNG, 100, out);
			cv.put("picture", out.toByteArray());
		}
		
		return cv;
	}
	
	void save(SQLiteDatabase db) {		

		db.insert("beer", "name", createContentValues());
	}
 
	void update(String id, SQLiteDatabase db) {		

		db.update("beer", createContentValues(), "_id=?", new String[]{id});
	}

	
}
